//
//  MLAFoodSearchVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/9.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAFoodSearchVC.h"
#import "MLABaseSearchView.h"
#import "MLAFoodHomeListModel.h"
#import "MLAShopFoodRightModel.h"
#import "MLAShopFoodRightCell.h"
#import "MLAFoodHomeListCell.h"
#import "MLAShopFoodsListVC.h"

@interface MLAFoodSearchVC ()

@property (nonatomic, strong) NSMutableArray<MLAFoodHomeListModel *> *shopLists;
@property (nonatomic, strong) NSMutableArray<MLAFoodHomeListModel *> *recLists;

@end

@implementation MLAFoodSearchVC {
    MLABaseSearchView *searchView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MLAShopFoodRightCell" bundle:nil] forCellReuseIdentifier:@"rightCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MLAFoodHomeListCell" bundle:nil] forCellReuseIdentifier:kCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"MLAFoodHomeListCell" bundle:nil] forCellReuseIdentifier:kCellID];

    
    self.tableView.backgroundColor = CX_GetColorLine();

    searchView = [[MLABaseSearchView alloc] init];
    searchView.canEdit = YES;
    [self.view addSubview:searchView];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(80);
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->searchView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
    
    @weakify(self);
    searchView.shouldDidClickSearch = ^{
        @strongify(self);
        [self searchKeyword:self->searchView.keyword];
    };

}

- (void)searchKeyword:(NSString *)keyword {

    if (keyword.length == 0) {
        [MLAHudManage showMessage:@"请输入搜索内容"];
        return;
    }

    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kAppProductList params:@{@"name":keyword} success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.shopLists = [NSMutableArray array];
        self.shopLists = [MLAFoodHomeListModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        
        self.recLists = [NSMutableArray array];
        self.recLists = [MLAFoodHomeListModel mj_objectArrayWithKeyValuesArray:response[@"recommend_shop"]];
        
        [self.tableView reloadData];

    } failure:^{
        
    }];
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_recLists.count == 0) {
        return _shopLists.count;
    } else {
        return _shopLists.count+1;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section < _shopLists.count) {
        MLAFoodHomeListModel *model = _shopLists[section];
        return model.product.count+1;
    } else {
        return _recLists.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section < _shopLists.count) {
        MLAFoodHomeListModel *model = _shopLists[indexPath.section];

        if (indexPath.row == 0) {
            MLAFoodHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = model;

            return cell;
        } else {
            MLAShopFoodRightModel *foodModel = model.product[indexPath.row-1];
            
            MLAShopFoodRightCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell" forIndexPath:indexPath];
            cell.addBtn.hidden = YES;
            cell.numLbl.hidden = YES;
            cell.subBtn.hidden = YES;
            cell.model = foodModel;
            
            return cell;
        }
        
    } else {
        MLAFoodHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        MLAFoodHomeListModel *model = _recLists[indexPath.row];
        cell.model = model;

        return cell;
    }
    
    return nil;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < _shopLists.count) {
        if (indexPath.row == 0) {
            return 68;
        } else {
            return 110;
        }
        
    } else {
        return 68;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section < _shopLists.count-1) {
        return 10;
    } else {
        return 0.01;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == _shopLists.count) {
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 0, CX_GetScreenWidth(), 30);
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = CX_GetNormalFont(12);
        [btn setTitleColor:CX_GetColorHex(0x333333, 1) forState:UIControlStateNormal];
        [btn setTitle:@" 为你推荐" forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"f_rec"] forState:UIControlStateNormal];
        [view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.height.offset(30);
            make.width.offset(100);
        }];
        
        UIView *leftLine = [[UIView alloc] init];
        leftLine.backgroundColor = CX_GetColorHex(0x333333, 1);
        [view addSubview:leftLine];
        [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(9);
            make.height.offset(1);
            make.centerY.offset(0);
            make.right.equalTo(btn.mas_left).offset(-5);
        }];
        
        UIView *rightLine = [[UIView alloc] init];
        rightLine.backgroundColor = CX_GetColorHex(0x333333, 1);
        [view addSubview:rightLine];
        [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(9);
            make.height.offset(1);
            make.centerY.offset(0);
            make.left.equalTo(btn.mas_right).offset(5);
        }];

        
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == _shopLists.count) {
        return 30;
    }
    return 0.001;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section < _shopLists.count) {
        MLAFoodHomeListModel *model = _shopLists[indexPath.section];
        
        MLAShopFoodsListVC *sflVC = [[MLAShopFoodsListVC alloc ] init];
        sflVC.model = model;
        [self.navigationController pushViewController:sflVC animated:YES];

    } else {
        MLAFoodHomeListModel *model = _recLists[indexPath.row];
        
        MLAShopFoodsListVC *sflVC = [[MLAShopFoodsListVC alloc ] init];
        sflVC.model = model;
        [self.navigationController pushViewController:sflVC animated:YES];

    }

    
}

@end
